const app = getApp();
// 封装wx.request函数
const request = (url,options) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${app.globalData.host}${url}`,
      method: options.method ? options.method : 'GET',
      data:options.method == 'GET' ? options.data:JSON.stringify(options.data),
      header: {
        'Content-Type': 'application/json; charset=UTF-8'
      },
      success(res) {
        if (res.data.status == 0) {
            resolve(res.data)
        } else {
            reject(res.data)
        }
      },
      fail(err) {
          reject(err)
      }
    })
  })
}
const get = (url, options = {}) => {
  return request(url, { method: 'GET', data: options })
}

const post = (url, options) => {
  return request(url, { method: 'POST', data: options })
}

const put = (url, options) => {
  return request(url, { method: 'PUT', data: options })
}

// 不能声明DELETE（关键字）
const remove = (url, options) => {
  return request(url, { method: 'DELETE', data: options })
}

module.exports = {
  get,
  post,
  put,
  remove
}
